﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StoragePeriod.Common;
using StoragePeriod.Data;

namespace StoragePeriod.Web.Nutritionknowledge.NutrientContent
{
    public partial class NutrientsintroEdit : SecurityPage
    {
        public override string PrivCode()
        {
            return "9a9f6d9a1be2b151";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.InitDr_Type();
                if (Request.QueryString["EditID"] != null && Request.QueryString["EditType"].ToString() == "Edit")
                {
                    this.LoadData();
                }
            }
        }

        //加载类别
        public void InitDr_Type()
        {
            this.dr_type.Items.Clear();
            this.dr_type.Items.Add(new ListItem("请选择分类", "0"));
            NutritionType.All().ToList().ForEach(p =>
            {
                ListItem li = new ListItem(p.nutrition, p.id.ToString());
                this.dr_type.Items.Add(li);
            });
        }

        //绑定数据
        private void LoadData()
        {
            int id = int.Parse(Request.QueryString["EditID"]); //知识点ID
            IntroduceNutrition intro = IntroduceNutrition.Where("id == @0", id).ToList().FirstOrDefault();
            if (intro != null)
            {
                this.dr_type.SelectedValue = intro.typeId.ToString();
                this.E_Title.Text = intro.title;
                this.FCK_Content.Value = intro.cText;
            }
        }


        protected void btPost_Click(object sender, EventArgs e)
        {
            IntroduceNutrition intro = new IntroduceNutrition();
            intro.title = this.E_Title.Text.Trim();
            intro.cText = this.FCK_Content.Value;
            intro.typeId = Convert.ToInt32(this.dr_type.SelectedValue);
            intro.dTime = DateTime.Now;
            intro.userId = 0;
            if (Request.QueryString["EditType"] == "Add") //添加
            {
                IntroduceNutrition.Insert(intro);
                ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script>alert('添加成功！');parent.location.href='/Nutritionknowledge/Nutrientsintro.aspx';</script>");
            }
            else //修改
            {
                intro.id = int.Parse(Request.QueryString["EditID"].ToString());
                IntroduceNutrition.Update(intro, "id=@0", intro.id);
                ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script>alert('修改成功！');parent.location.href='/Nutritionknowledge/Nutrientsintro.aspx';</script>");
            }
        }
    }
}